<!DOCTYPE html>
<html lang="en">
    <head>
        
        <script src="jquery-1.11.0.js"></script>
        <script src="gen.js"></script>
        <link rel="stylesheet" href="style.css">
        <link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:700' rel='stylesheet' type='text/css'>
        <title>Health Promotion</title>
        <script>
     function syncAjax(u){
				var obj=$.ajax(
					{url:u,
					 async:false
					 }
				);
				return $.parseJSON(obj.responseText);
				
			}  
                        
                        
    function add() {
               
                var y = event.clientY;
                var x = event.clientX / 2;
                $("#divAdd").css("top", y);
                $("#divAdd").css("left", x);
                
                $("#divAdd").fadeIn(250);
                
            }        
    
            var qid;
            function view(id, rowid) {
                
                var div = "#answerDiv"+rowid;
                qid = id;
                var r=getanswers(id);
				if(r.result!=0){
                                    
				for(var i = 0; i<r.answer.length; i++)
                         {
                             $(div).append(r.answer[i].name + "<br>");
                         }	
				}
                                
                         
                         $(div).append($("#addAnswer"));
                var y = event.clientY;
                var x = event.clientX/2;
               
                $(div).css("top", y);
                $(div).css("left", x);      
                $(div).fadeIn(250);
            }
            
            function addAnswer()
            {
                var ans = document.getElementById("answer").value;
                var u="vaccine_action.php?cmd=5&ans="+ans+"&qid="+qid;
                return syncAjax(u);
            }
            
            function getanswers(qid){
				var u="vaccine_action.php?cmd=3&id="+qid;
				return syncAjax(u);
			}
            
            function cancel(){
                            $("#divAdd").fadeOut(500);
				
				
			}
        </script>
    </head>

    <body id = "background">
        <?php
        include_once("category.php");
        include_once("questionanswerclss.php");
        include_once 'healthclass.php';
        include_once 'subdistrict.php';
        include_once 'choclass.php';
        include_once 'question.php';
        ?>
        
        <div id = "homepage">
            <table style ="float: left; width: 150px">

                <tr>
                    <td>
                        <div id = "navigation">
                        </div>  
                    </td>
                </tr>

            </table>
            <table>
                <tr>

                    <td>
                        <div id = "content">
                            <!--style="background-color: #558C89"-->
                            <div id="pageheader" style="padding-top: 20px; border">
                                <img src ="Images/healthrealPAINT.jpg">
                            </div>
                            <br>
                            <br>
                            <br>
                            <br>
                            <br>
                             <div id = "searchBar">
                                <div style="float: left">
                                    <input  type="button" onclick="add()" id = "button" value = "Add a question" name = "add">
                                </div>
                                  
                                 <div style="float: left" >
                                   <form id ="searchAdd">
                                    <input type="text" name ="tosearch" id = "searchText">
                                    <input type="submit" id = "button" value = "Search" name = "search">
                                   </form>
                                 </div>
                            </div>
                            
                            <div style="float: left; padding-left: 20px ">
                                <a class="mainMenu" href="index_1.php">Dashboard</a>
                                <a class="mainMenu" href="health.php">Health Promotions</a>
                                <a class="mainMenu" href="charts.php">Questions And Answers</a>

                            </div>
                            
                            <br>
                            
                            <div style="float: left; padding-left: 100px">
                                <?php
        
        $question = new question();
        $row_counter = 1;
          if(isset($_REQUEST['tosearch']))
        {
            $question->search($_REQUEST['tosearch']);
            
            $row = $question->fetch();
            echo "<br>";
            while($row)
            {
              echo"<div id='answerDiv$row_counter' class='answerDiv'>";
              echo"$row_counter . $row[name]";
              echo"<br>";
              echo"<span id ='hideDiv' class='hotspot' style='padding-left: 470px' onclick ='view($row[qid],$row_counter')'>View Answers<span>";
              echo"<br>"; 
              echo"</div>";
            
            echo"<br>";
            $row_counter++;
            $row = $question->fetch();
            
            }
             
        }  
        
 else
     {
     $question->get_all_questions();
            $row = $question->fetch();
            $row_counter = 1;
           
            echo "<br>";
            
            while($row)
            {
                
            
            echo"<div id='answerDiv$row_counter' class='answerDiv'>";
            echo"$row[name]"; 
            echo"<br>";
            echo"<span class='hotspot' style='padding-left: 470px' onclick ='view($row[qid],$row_counter)'>View Answers<span>";
            echo"<br>";
            echo"</div>";
           
            
            echo"<br>";
            $row_counter++;
            $row = $question->fetch();
            }
        
     }
        ?>
                                
                                </div>
                        </div>
                    </td>
                </tr>
            </table>
            
            
         </div>
        <div id = 'divAdd' class='popupForm'>
            <div class="col-lg-6">

            <form role="form" style ="padding-left: 15px;  padding-top: 15px">

              <div class="form-group">
                <label class="label"> Enter Question</label><br>
                <textarea class = "form-control" name = "question" rows="7" cols ="55"> </textarea>
               
              </div>

              <div class="form-group">
                <label class="label">Choose a category</label>
                
                <?php
                $obj = new category();
                if(!$obj->get_all_categories())
                 {
                    echo"cannot find categories";
                 }
                 
                else
                 {
                    
                    echo"<select class='form-control' name='category'>";
                    $row = $obj->fetch();
                    $mane = "";
                    while ($row)
                      {
                      $name = ucfirst($row[name]);
                      echo "<option value=$row[cid]>$name</option>";
                      $row = $obj->fetch();
                 }
                    echo"</select>";
                 }
                ?>
              </div>


               <div id ="subCancel">
                            <input type="submit" value = "Submit">
                            <input type="button" value="Cancel" onclick="cancel()" >
                            </div>
              </form>

          </div>
            </div>
        
        <?php
    if(isset($_POST))
    {
        if(isset($_REQUEST))
    {
        $cid = '';
        $name = "";
        if(isset($_REQUEST['category']))
        {
            $cid = $_REQUEST['category'];
        } 
        
        if(isset($_REQUEST['name']))
        {
            $name = $_REQUEST['name']; 
        }
        
        if(isset($_REQUEST['question']))
        {
          $question = new question();
        if(!$question->add_question('1',$cid,$_REQUEST['question']))
        {
            echo "cannot add";
        }  
        }
        
    }
    }
    
   
    ?>
           
        <div id="addAnswer">
            <form>
                <div class="form-group">
                <textarea class = "form-control" id ="answer" name = "answer" rows="7" cols ="55"> </textarea>
              </div>
                <input type ="submit" value = "Add answer" onclick="addAnswer()">
            </form>
            
            </div>
                
                
    </body>
</html>


  